package com.my.app.tools.core.service.common;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.my.app.tools.pojo.AppConstants;
import com.my.app.tools.util.LogUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

public class AppConfigService {

    /**
     * 读取参数
     *
     * @param configPath
     * @return
     * @author guochaohui
     * @date 2020-04-03 16:30
     */
    public static List<String> readListConfig(String configPath) {
        List<String> configs = Lists.newArrayList();
        try {
            if (document == null) {
                return configs;
            }
            List nodes = document.getRootElement().selectNodes(configPath);
            if (CollectionUtils.isNotEmpty(nodes)) {
                for (Object obj : nodes) {
                    Node node = (Node) obj;
                    configs.add(node.getText().trim());
                }
            }
            return configs;
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error(e.getMessage());
            return configs;
        }
    }

    /**
     * 读取参数
     *
     * @param configPath
     * @return
     * @author guochaohui
     * @date 2020-04-03 16:30
     */
    public static Boolean readBooleanConfig(String configPath) {
        try {
            String value = readStringConfig(configPath);
            return Boolean.valueOf(value);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error(e.getMessage());
            return null;
        }
    }

    /**
     * 读取参数
     *
     * @param configPath
     * @return
     * @author guochaohui
     * @date 2020-04-03 16:30
     */
    public static String readStringConfig(String configPath) {
        try {
            if (document == null) {
                return null;
            }
            Node node = document.getRootElement().selectSingleNode(configPath);
            return node.getText().trim();
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error(e.getMessage());
            return null;
        }
    }

    private static Document document;

    static {
        reloadConfig();
    }

    public static void reloadConfig() {
        try {
            String rootPath = null;
            // 读取classPath
            String mainPath = System.getProperty("java.class.path");
            // 按;分割路径
            List<String> allPaths = Splitter.on(";").splitToList(mainPath);
            if (CollectionUtils.size(allPaths) == 1) {
                // 如果只有一个路径直接取
                rootPath = allPaths.get(0);
            } else if (CollectionUtils.size(allPaths) > 1) {
                for (String path : allPaths) {
                    // 如果有多个路径，取文件夹路径
                    if (new File(path).isDirectory()) {
                        rootPath = path;
                        break;
                    }
                }
            }
            File appFile = new File(rootPath);
            if (appFile.isDirectory()) {
                rootPath = appFile.getAbsolutePath();
            } else {
                // 路径不是文件夹是jar的路径，取父级路径
                rootPath = appFile.getParentFile().getAbsolutePath();
            }
            String configFilePath = rootPath.concat("/").concat(AppConstants.MY_TOOLS_APP_CONFIG_XML_FILE_NAME);
            File configFile = new File(configFilePath);
            if (configFile.exists()) {
                String configText = IOUtils.toString(new FileInputStream(configFile), AppConstants.ENCODING_UTF8);
                LogUtil.info("\r\n========== config begin ==========\r\n"
                        + configFilePath
                        + "\r\n"
                        + configText
                        + "\r\n========== config end ============\r\n");
                // 加载配置文件
                SAXReader saxReader = new SAXReader();
                saxReader.setEncoding(AppConstants.ENCODING_UTF8);
                document = saxReader.read(configFile);
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error(e.getMessage());
        }
    }

}
